package com.sl.core.engine.plugin;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;
import org.apache.hop.core.plugins.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProcessStepPluginType extends TransformPluginType {
    private static ProcessStepPluginType stepPluginType;

    protected ProcessStepPluginType() {
        super();
    }

    public static ProcessStepPluginType getInstance() {
        if (stepPluginType == null) {
            stepPluginType = new ProcessStepPluginType();
        }
        return stepPluginType;
    }


    @SneakyThrows
    public void register(String orgId,Class<?> clazz, String cat, String id, String version, Boolean latest, String name, String desc, String image) {
        if (StrUtil.isBlank(version)) {
            version = "1.0.0";
        }
        List<String> ids = Lists.newArrayList();
        if (BooleanUtil.isTrue(latest)) {
            ids.add(id);
            ids.add(ProcessPluginRegistry.getId(orgId,id, version));
        } else {
            //不是的话 就要代入版本
            ids.add(ProcessPluginRegistry.getId(orgId,id, version));
        }
        Class<? extends IPluginType> pluginType = TransformPluginType.class;
        Map<Class<?>, String> classMap = new HashMap<>();
        PluginMainClassType mainClassTypesAnnotation = pluginType.getAnnotation(PluginMainClassType.class);
        classMap.put(mainClassTypesAnnotation.value(), clazz.getName());
        ProcessSupplementalPlugin stepPlugin = new ProcessSupplementalPlugin(ids.toArray(new String[0]), pluginType, mainClassTypesAnnotation.value(), cat, name, desc, image, false, false, classMap, Lists.newArrayList(), null, null, null, false,clazz);
        registry.registerPlugin(pluginType, stepPlugin);
    }
}
